public class MyString {
    private final char[] value;

    public MyString(char[] chars) {
        value = new char[chars.length];
        System.arraycopy(chars, 0, value, 0, chars.length);
    }

    public MyString(String s) {
        this(s.toCharArray());
    }

    public char chatAt(int index) {
        if (index < 0 || index >= value.length) return (char) 0;
        return value[index];
    }

    public int length() {
        return value.length;
    }

    public MyString substring(int begin, int end) {
        //处理begin和end的特殊取值
        //一般地，要求0<=begin<begin+1<=end<=value.length
        //若begin==end，返回整个字符串
        if (begin == end) return new MyString(value);
        //若begin>end，两者互换，互换后有begin+1<=end
        if (begin > end) {
            int tmp = begin;
            begin = end;
            end = tmp;
        }
        //若begin<0或end>=value.length，自动归约到0和value.length
        if (begin < 0) begin = 0;
        if (end > value.length) end = value.length;

        int len = end - begin;
        char[] newCharArr = new char[len];
        for (int i = 0; i < len; i++) newCharArr[i] = value[i + begin];
        return new MyString(newCharArr);
    }

    public MyString substring(int begin) {
        if (begin >= value.length) return null;
        if (begin < 0) begin = 0;
        return substring(begin, value.length);
    }

    public MyString toLowerCase() {
        char[] newArr = new char[value.length];
        for (int i = 0; i < value.length; i++) {
            if (value[i] >= 'A' && value[i] <= 'Z') {
                newArr[i] = (char) (value[i] + 32);
            }
            else newArr[i] = value[i];
        }
        return new MyString(newArr);
    }

    public MyString toUpperCase() {
        char[] newArr = new char[value.length];
        for (int i = 0; i < value.length; i++) {
            if (value[i] >= 'a' && value[i] <= 'z') {
                newArr[i] = (char) (value[i] - 32);
            }
            else newArr[i] = value[i];
        }
        return new MyString(newArr);
    }

    public boolean equals(MyString s) {
        if (value.length != s.value.length) return false;
        for (int i = 0; i < value.length; i++)
            if (value[i] != s.value[i]) return false;
        return true;
    }

    public static MyString valueOf(int i) {
        int length = 0;
        char[] newArr;
        if (i == 0) {
            newArr = new char[1];
            newArr[0] = '0';
            return new MyString(newArr);
        }
        //统计长度
        int remainder = Math.abs(i);
        while (remainder > 0) {
            remainder /= 10;
            length++;
        }
        //分配空间
        int beginIndex;
        if (i > 0) {
            newArr = new char[length];
            beginIndex = 0;
        }
        else {
            length++;
            newArr = new char[length];
            newArr[0] = '-';
            beginIndex = 1;
        }
        //装填内容
        remainder = Math.abs(i);
        int digit;
        for (int j = length - 1; j >= beginIndex; j--) {
            digit = remainder % 10;
            remainder /= 10;
            newArr[j] = (char) (digit + '0');
        }
        return new MyString(newArr);
    }

    public static MyString valueOf(boolean b) {
        if (b == true) return new MyString("true");
        else return new MyString("false");
    }

    public int compare(String s) {
        int length = Math.min(s.length(), value.length);
        for (int i = 0; i < length; i++)
            if (value[i] < s.charAt(i)) return -1;
            else if (value[i] > s.charAt(i)) return 1;
        //[0,length-1]范围内完全相等
        if (s.length() == value.length) return 0;
        else if (s.length() > value.length) return -1;
        else return 1;
    }

    public int compare(MyString ms) {
        int length = Math.min(ms.length(), value.length);
        for (int i = 0; i < length; i++)
            if (value[i] < ms.value[i]) return -1;
            else if (value[i] > ms.value[i]) return 1;
        //[0,length-1]范围内完全相等
        if (ms.length() == value.length) return 0;
        else if (ms.length() > value.length) return -1;
        else return 1;
    }

    public char[] toCharArray() {
        char[] ret = new char[value.length];
        System.arraycopy(value, 0, ret, 0, value.length);
        return ret;
    }

    public void print() {
        System.out.println(value);
    }

    public static void main(String args[]) {
        String str = "Hello.";
        char[] arr = str.toCharArray();
        MyString ms1 = new MyString(str);
        MyString ms2 = new MyString(arr);
        System.out.println(ms1.equals(ms2)); //期待结果：true
        System.out.println(ms1.compare(ms2)); //期待结果：0
        System.out.println(ms1.compare(str)); //期待结果：0
        MyString upper = ms1.toUpperCase(); //期待结果：HELLO.
        upper.print();
        MyString lower = ms1.toLowerCase(); //期待结果：hello.
        lower.print();
        System.out.println(ms1.compare(lower)); //期待结果：-1
        MyString ms3 = new MyString("Hello, welcome to Hoenn.");
        System.out.println(ms1.substring(0, 5).equals(ms3.substring(0, 5))); //期待结果：true
        MyString.valueOf(0).print(); //期待结果：0
        MyString.valueOf(-1).print(); //期待结果：-1
        MyString.valueOf(100).print(); //期待结果：100
        MyString.valueOf(true).print(); //期待结果：true
        System.out.println(ms3.toCharArray()); //期待结果：Hello, welcome to Hoenn.
    }
}
